package com.zzx.controller;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LayuiController {
	
	@RequestMapping("/test")
	public String onoLayui() {
		System.err.println("*******跳转页面展示layui***************");
		return "test";
	}
	@RequestMapping("/tablist")
	public String tabLayuiList() {
		System.err.println("*******table 展示 layui***************");
		return "tablist";
	}
	@RequestMapping("/piclun")
	public String layuiPic() {
		System.err.println("******轮播图 展示 layui***************");
		return "piclun";
	}
	@RequestMapping("/ccPlay")
	public String ccPlay(Model model) {
		System.err.println("******cc 直播 layui***************");
		model.addAttribute("roomId","F4CD4BA5E71181AD9C33DC5901307461");
		model.addAttribute("userId","41E91C81E48CAD28");
		model.addAttribute("userName","好贱同学");
		model.addAttribute("pwd","123456");
		return "ccPlay";
	}
	@RequestMapping("/message")
	public String message(Model model) {
		System.err.println("******message 展示 layui***************");		
		int num = (int) (Math.random()*999/1000);
		 model.addAttribute("msageRtn",num);
		return "message";
	}
	@RequestMapping(value="/randomName",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> randomName(Model model) {
		//model.addAttribute("rtnMsg","asdfsd");
		Map<String,Object> obj = new HashMap<String,Object>();
		obj.put("rtnMsg", "北国风光，千里冰封，万里雪飘。望长城内外，惟余莽莽；大河上下，顿失滔滔。山舞银蛇，原驰蜡象，欲与天公试比高。须晴日，看红装素裹，分外妖娆。\r\n" + 
				"江山如此多娇，引无数英雄竞折腰。惜秦皇汉武，略输文采；唐宗宋祖，稍逊风骚。一代天骄，成吉思汗，只识弯弓射大雕。俱往矣，数风流人物，还看今朝。");
		return obj;
	}
	@RequestMapping(value="/getculture",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getculture(Model model) {
		Map<String,Object> obj = new HashMap<String,Object>();
		obj.put("rtnMsg", "乾 ：甲 壬\r\n坎：戊\r\n艮：丙\r\n震：庚\r\n" + 
				"巽 ：辛\r\n离：己\r\n坤：乙 癸\r\n兑：丁\r\n" + 
				"乾：子 寅 辰 午 申 戌\r\n坎：寅 辰 午 申 戌 子\r\n艮：辰 午 申 戌 子 寅\r\n震：子 寅 辰 午 申 戌\r\n"+
				"巽：丑 亥 酉 未 巳 卯\r\n离：卯 丑 亥 酉 未 巳\r\n坤：未 巳 卯 丑 亥 酉\r\n兑：巳 卯 丑 亥 酉 未");
		return obj;
	}
	@RequestMapping(value="/getbook",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getbook(Model model) throws URISyntaxException {
		Map<String,Object> obj = new HashMap<String,Object>();
		List<Map<String,String>> list = getDom4jXml();
		obj.put("rtnCd", "00");
		obj.put("rtnMsg", "数据加载成功");
		obj.put("data", list);
		return obj;
	}
	@RequestMapping(value="/deliveryMsg",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> deliveryMsg(Model model) {
		//model.addAttribute("rtnMsg","asdfsd");
		Map<String,Object> obj = new HashMap<String,Object>();
		obj.put("rtnMsg", Str(10));
		return obj;
	}
	/// </summary>    
    /// <param name="Length">生成长度</param>    
    public static String Str(int Length)    
    {    
        char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 
        		'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        		'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };    
        String result = "";    
        Random random = new Random();    
        for (int i = 0; i < Length; i++)    
        {    
            int rnd = random.nextInt(36);    
            result += Pattern[rnd];    
        }    
        return result;    
    }    
	public String getTime(Model model) {
		System.err.println("******message 展示 layui***************");
		 int num = (int) (Math.random()/1000);
		 model.addAttribute("msageRtn",num);
		return "message";
	}
	@RequestMapping("/upload")
	public String layuiupload() {
		System.err.println("*******文件上传 展示 layui***************");
		return "upload";
	}
	@RequestMapping("/list")
	public String onoLayuiList() {
		System.err.println("*******list跳转页面展示layui***************");
		return "list";
	}
	@RequestMapping("/add")
	public String onoLayuiAdd() {
		System.err.println("*******新增页面展示layui***************");
		return "add";
	}
	@RequestMapping(value="/listData",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> onoLayuiList(Model model) {
		System.err.println("*******列表跳转页面展示layui***************");
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		Map<String,String> map = new HashMap<String,String>();
		map.put("name", "aaaaaaa");
		map.put("age", "22");
		map.put("id", "111");
		Map<String,String> map1 = new HashMap<String,String>();
		map1.put("name", "aaaaaaa");
		map1.put("age", "22");
		map1.put("id", "111");
		Map<String,String> map2 = new HashMap<String,String>();
		map2.put("name", "李四");
		map2.put("age", "50");
		map2.put("id", "2222");
		Map<String,String> map3 = new HashMap<String,String>();
		map3.put("name", "张三");
		map3.put("age", "33");
		map3.put("id", "333");
		list.add(map);
		list.add(map1);
		list.add(map2);
		list.add(map3);
		//model.addAllAttributes(map);
		model.addAttribute(map);
		Map<String,Object> obj = new HashMap<String,Object>();
		Map<String,Object> obj1 = new HashMap<String,Object>();
		Map<String,Object> obj2 = new HashMap<String,Object>();
		obj2.put("total", 105);
		//page.total,
		//"data": res.data.datas
		obj1.put("datas", list);
		obj1.put("page", obj2);
		//obj1.put("total", 105);
		obj.put("code", "0");
		obj.put("count", 105);
		obj.put("data", obj1);
		obj.put("status", "success");
		obj.put("message", "成功");
		//"count": res.data.page.total,
		///"data": res.data.datas
		return obj;
	}
	private List<Map<String,String>> getDom4jXml() throws URISyntaxException {
		System.err.println("*******"+this.getClass().getClassLoader().getResource("/").toURI().getPath());
		List<Map<String,String>> bookList = new ArrayList<Map<String,String>>();
		// 解析books.xml文件
        // 创建SAXReader的对象reader
        SAXReader reader = new SAXReader();
        try {
            // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。\\src\\main\\resources\\xml/books.xml
        	//new BufferedInputStream(new FileInputStream("src/main/resources/text.properties"));
            Document document = reader.read(new File(this.getClass().getClassLoader().getResource("/").toURI().getPath()+"/xml/books.xml"));
            // 通过document对象获取根节点bookstore
            Element bookStore = document.getRootElement();
            // 通过element对象的elementIterator方法获取迭代器
            Iterator it = bookStore.elementIterator();
            // 遍历迭代器，获取根节点中的信息（书籍）
            while (it.hasNext()) {
                Map<String,String> map = new HashMap<String,String>();
            	System.out.println("=====开始遍历某一本书=====");
                Element book = (Element) it.next();
                // 获取book的属性名以及 属性值
                List<Attribute> bookAttrs = book.attributes();
                for (Attribute attr : bookAttrs) {//读取一级节点 属性名和值                   
                    map.put(attr.getName(), attr.getValue());
                	System.out.println("属性名：" + attr.getName() + "--属性值："
                            + attr.getValue());
                }
                Iterator itt = book.elementIterator();
                while (itt.hasNext()) {//读取下级节点里面的值。                    
                	Element bookChild = (Element) itt.next();
                	System.out.println("节点名：" + bookChild.getName() + "--节点值：" + bookChild.getStringValue());
                    map.put(bookChild.getName(), bookChild.getStringValue());
                    
                }
                bookList.add(map);
                System.out.println("=====结束遍历某一本书=====");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bookList;       
	}
}
